﻿#include "scaadispatcher.h"

SCAADispatcher::SCAADispatcher(MasterMotionManager *masterMotionManager) : masterMotionManager(masterMotionManager)
{
    uphHelper = new UPHHelper();

    lensTrayConfig = new TrayConfig();
    lensTrayConfigFile = new ConfigFile("LensTrayConfig", lensTrayConfig,
                                        QString("%1/LensTrayConfig.json").arg(Worker::getWorkerConfigDir()));
    lensTrayConfigFile->populate();
    lensTrayMap = new LensTrayMap(lensTrayConfig, QString("%1/LensTrayMap.json").arg(Worker::getWorkerConfigDir()));

    sensorTrayConfig = new TrayConfig();
    sensorTrayConfigFile = new ConfigFile("SensorTrayConfig", sensorTrayConfig,
                                          QString("%1/SensorTrayConfig.json").arg(Worker::getWorkerConfigDir()));
    sensorTrayConfigFile->populate();
    sensorTrayMap = new SensorTrayMap(sensorTrayConfig, "SensorTrayMap",
                                      QString("%1/SensorTrayMap.json").arg(Worker::getWorkerConfigDir()));

    rejectTrayConfig = new TrayConfig();
    rejectTrayConfigFile = new ConfigFile("RejectTrayConfig", rejectTrayConfig,
                                          QString("%1/RejectTrayConfig.json").arg(Worker::getWorkerConfigDir()));
    rejectTrayConfigFile->populate();
    rejectTrayMap = new SensorTrayMap(rejectTrayConfig, "RejectTrayMap",
                                      QString("%1/RejectTrayMap.json").arg(Worker::getWorkerConfigDir()));

    pickArm = new PickArm(sensorTrayConfig, lensTrayConfig, rejectTrayConfig, sensorTrayMap, lensTrayMap, rejectTrayMap);

    aaCore = new AACore();
    aaImage = new AAImage();
    aaHead = new AaHead(aaCore);

    aaCore->setAAHead(aaHead);
    pressureMonitor = new PressureMonitor();

    registerWorker(aaCore);
    registerWorker(pickArm);
    registerWorker(aaHead);
    registerWorker(pressureMonitor);
}

void SCAADispatcher::setContextProperty(QQmlApplicationEngine &engine)
{
    engine.rootContext()->setContextProperty("uphHelper", uphHelper);

    engine.rootContext()->setContextProperty("sensorTrayConfig", sensorTrayConfig);
    engine.rootContext()->setContextProperty("lensTrayConfig", lensTrayConfig);
    engine.rootContext()->setContextProperty("rejectTrayConfig", rejectTrayConfig);
    engine.rootContext()->setContextProperty("lensTrayMap", lensTrayMap);
    engine.rootContext()->setContextProperty("sensorTrayMap", sensorTrayMap);
    engine.rootContext()->setContextProperty("rejectTrayMap", rejectTrayMap);

    engine.rootContext()->setContextProperty("pickArm", pickArm);
    engine.rootContext()->setContextProperty("paConfig", pickArm->getPaConfig());

    engine.rootContext()->setContextProperty("aaHead", aaHead);
    engine.rootContext()->setContextProperty("aaConfig", aaHead->getAAHeadConfig());
    engine.rootContext()->setContextProperty("dispensorConfig",aaHead->getAAHeadConfig()->dispenserConfig());
    engine.rootContext()->setContextProperty("glueCheckParam", aaHead->getAAHeadConfig()->glueCheckParam());

    //AACore
    engine.rootContext()->setContextProperty("aaCore", aaCore);
    engine.rootContext()->setContextProperty("aaCoreConfig", aaCore->getAACoreConfig());
    engine.rootContext()->setContextProperty("aaImage", aaImage);

    engine.rootContext()->setContextProperty("dataFromCpp1", &aaCore->aaData_1);
    engine.rootContext()->setContextProperty("dataFromCpp2", &aaCore->aaData_2);
    engine.rootContext()->setContextProperty("mtfLoopTestData", &aaCore->mtfLoopTestData);
    connect(aaCore, &AACore::displayAAImageInUI, aaImage, &AAImage::receiveAAImage, Qt::DirectConnection);
    //Image Provider
    engine.addImageProvider(QLatin1String("liveImage"), aaCore->liveImageProvider);
    engine.addImageProvider(QLatin1String("mtfImage"), aaCore->mtfImageProvider);
    engine.addImageProvider(QLatin1String("ocImage"), aaCore->ocImageProvider);
    engine.addImageProvider(QLatin1String("aaDebugImage"), aaCore->aaDebugImageProvider);

    //End of AA Core
}

void SCAADispatcher::handleInit()
{
    VisionManager::getIns().postInit();
    masterMotionManager->initMotionManagers();
}

void SCAADispatcher::handleStart()
{
    uphHelper->start();
}

void SCAADispatcher::handlePause()
{
    QThread::msleep(50);
    masterMotionManager->pause();
}

void SCAADispatcher::handleContinue()
{
    masterMotionManager->unPause();
}

void SCAADispatcher::handleAbort()
{
    masterMotionManager->handleAbort();
}

void SCAADispatcher::handleReset()
{
    masterMotionManager->handleReset();
}

void SCAADispatcher::preHome(){}
